# -*- coding: utf-8 -*-
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ServerFactory
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.protocols.basic import LineReceiver

from config_reader import ConfigReader

class FileManager():
    @staticmethod
    def readFile(filePath):
        try:
            fp = open(filePath, 'r')
        
            content = fp.read()
        
            if not content:
                print 'emputy file: %s' % filePath
                
                fp.close()
                return None
            
            fp.close()
            return content
        
        except IOError:
            if fp:
                fp.close()
                
                print 'IOError'
                
                return None

class PyWebServer_Protocol(LineReceiver):
    def lineReceived(self, line):
        #解析HTTP请求
        if line.startswith('GET'):
        
            req_type, req_path, http_version = line.split(' ')
            
            print '[debug]request type: %s' % req_type
            print '[debug]request path: %s' % req_path
            print '[debug]http_version: %s' % http_version
            
            if req_type != 'GET':
                print 'Only support GET'
                return
            
            if http_version != 'HTTP/1.1':
                print 'Only support HTTP/1.1'
                return
            
            if req_path == '/':
                local_path = r'../html/index.html'
                content = FileManager.readFile(local_path)
                
                print content
                
                self.transport.write(content + '\r\n')
                
                self.transport.loseConnection()
                
            else:
                local_path = r'../' + req_path
                
                content = FileManager.readFile(local_path)
                
                print content
                
                self.transport.write(content + '\r\n')
                
                self.transport.loseConnection()
            
            


class protocolFactory(ServerFactory):
    def __init__(self):
        pass
    
    def buildProtocol(self, addr):
        return PyWebServer_Protocol()
    


class Py_WebServer():
    def __init__(self):
        self.config_reader = ConfigReader()
        
        self.ip = self.config_reader.getConfigItem('listen_ip')
        self.port = int(self.config_reader.getConfigItem('listen_port'))
        self.root = self.config_reader.getConfigItem('root')
        self.version = self.config_reader.getConfigItem('version')
        
        if self.ip == '127.0.0.1':
            print 'EQUAL'
    
    def start(self):
        server_ep = TCP4ServerEndpoint(reactor, self.port, 50, '127.0.0.1')
        if not server_ep:
            print '[start]call TCP4ServerEndpoint failed'
            
            return
        
        server_ep.listen(protocolFactory())
        
        print 'Py_WebServer %s Started...' % self.version
        
        reactor.run()
        